<template>
  <el-dialog :title="showTitle" :visible="showDialog" @close="handelClose">
    <!-- 表单 -->
    <el-form ref="deptForm" label-width="120px" :model="deptForm" :rules="deptRules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="deptForm.name" size="mini" style="width: 80%;" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="deptForm.code" size="mini" style="width: 80%;" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <!-- 下拉列表 -->
        <el-select v-model="deptForm.managerId" size="mini" style="width: 80%;" placeholder="请选择负责人">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="deptForm.introduce" :rows="4" type="textarea" size="mini" style="width: 80%;" placeholder="1-100个字符" />
      </el-form-item>
      <el-form-item>
        <!-- 水平居中 -->
        <el-row style="width: 80%;" type="flex" justify="center">
          <el-button type="primary" size="mini" @click="confirmClick">确认</el-button>
          <el-button size="mini" @click="handelClose">取消</el-button>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addDepartmentAPI, getDepartmentDetailAPI, getDepartmentsAPI, getManagesAPI, updateDepartmentAPI } from '@/api/department'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    parentId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      managerList: [],
      deptForm: {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: '',
        id: ''
      },
      // 校验
      deptRules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称为2-10为字符', trigger: 'blur' },
          {
            validator: async(rule, value, callbck) => {
              let deptList = await getDepartmentsAPI()
              if (this.deptForm.id) {
                deptList = deptList.filter(item => item.id !== this.deptForm.id)
              }
              if (deptList.some(item => item.name === value)) {
                callbck(new Error('部门名称已存在'))
              } else {
                callbck()
              }
            },
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门编码为2-10为字符', trigger: 'blur' },
          {
            validator: async(rule, value, callbck) => {
              let deptList = await getDepartmentsAPI()
              if (this.deptForm.id) {
                deptList = deptList.filter(item => item.id !== this.deptForm.id)
              }
              if (deptList.some(item => item.code === value)) {
                callbck(new Error('部门编码已存在'))
              } else {
                callbck()
              }
            },
            trigger: 'blur'
          }
        ],
        managerId: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }

        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '部门介绍为1-100为字符', trigger: 'blur' }
        ]
      }
    }
  },
  // 计算属性
  computed: {
    showTitle() {
      return this.deptForm.id ? '编辑部门' : '添加部门'
    }
  },
  created() {
    // 获取部门负责人列表数据
    this.getManagerList()
  },
  // 方法
  methods: {
    // 获取部门详情
    async getDepartmentDetail() {
      // console.log(this.parentId)
      this.deptForm = await getDepartmentDetailAPI(this.parentId)
    },
    // 确认提交
    confirmClick() {
      this.$refs.deptForm.validate(async isOK => {
        if (isOK) {
          if (this.deptForm.id) {
            // 编辑模式
            // 修改部门数据
            await updateDepartmentAPI(this.deptForm)
            this.$message.success('编辑部门成功')
          } else {
            // 新增模式
            // 调接口添加子部门数据
            await addDepartmentAPI({ ...this.deptForm, pid: this.parentId })
            this.$message.success('添加子部门成功')
          }

          // 通知父级
          this.$emit('update')
          // 关闭弹层 + 清空表单
          this.handelClose()
        }
      })
    },
    // 调用部门负责人列表接口反法
    async getManagerList() {
      const managerList = await getManagesAPI()
      this.managerList = managerList
    },
    // 关闭弹层
    handelClose() {
      // 清空表单
      this.$refs.deptForm.resetFields()
      // 还原数据
      this.deptForm = {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: '',
        id: ''
      }
      // 配合sync修饰符的写法
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style>

</style>
